from CSYNAC.FunBase import FunsBase
from CSYNAC.FunNode import NodeFuns
import xmlrpc.client as Client


class CentralFuns(FunsBase):
    def node_reg(self, nodename, hostname, host_ip, port):
        super(CentralFuns,self).node_reg(nodename,hostname,host_ip,port)
        Nodes = self.memery.node_keeper.all_node()
        for node in Nodes:
            if not node == self.memery.nodename:
                if not node == nodename:
                    Node = Nodes[node]
                    with Client.ServerProxy(f'http://{Node.hostname}:{Node.port}') as client:
                        client:FunsBase
                        client.node_reg(nodename,hostname,host_ip,port)
                else:
                    pass
                    # 本来想把Central中的所有节点推送给注册方的结果想了想单线程应该没法处理这个玩意,先让他自己fetch吧
                    # 等到多线程了再来写这个.

    def node_delete(self,nodename):
        super(CentralFuns,self).node_delete(nodename)
        for node in self.memery.node_keeper.all_node():
            with Client.ServerProxy(f"http://{node.nodename}:{node.port}") as client:
                client:FunsBase
                client.node_delete(nodename)

    def node_central_down(self):
        for node in self.memery.node_keeper.all_node():
            with Client.ServerProxy(f"https://{node.host_ip}:{node.port}") as client:
                client:NodeFuns
                client.central_down()
